package com.dzsw.datastruct.tree.bst;

import com.dzsw.datastruct.tree.binary.Visitor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;

public class Main {
    private static Logger logger = LogManager.getLogger(com.dzsw.Main.class);

    public static void main(String[] args) {
        Configurator.initialize("Main", Main.class.getClassLoader(), "classpath:log4j2.xml");
        test1();

    }

    public static void test1() {
        // data
        int[] data = {7,4,9,2,5,8,11,3,12,1};
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        // add
        for (Integer i : data) {
            bst.add(i);
        }
        // print tree
        logger.info("Tree\n{}", bst.toString());
        logger.info("Tree height=[{}]", bst.height());
        // delete
        bst.remove(1);
        logger.info("Tree\n{}", bst.toString());
        bst.remove(7);
        logger.info("Tree\n{}", bst.toString());
        // container
        int num = 8;
        if (bst.contains(num)) {
            logger.info("contain [{}]", num);
        } else {
            logger.info("don't contain [{}]", num);
        }
        // is complete binary tree
        if (bst.isComplete()) {
            logger.info("This tree is a complete tree.");
        } else {
            logger.info("This tree is not a complete tree.");
        }
        // is proper binary tree
        if (bst.isProper()) {
            logger.info("This tree is a proper tree.");
        } else {
            logger.info("This tree is not a proper tree.");
        }
        // 前序遍历
        logger.info("================== [pre] ==================");
        final StringBuilder builder = new StringBuilder();
        bst.preOrder(new Visitor<>() {
            @Override
            protected boolean visit(Integer element) {
                builder.append(element).append(" ");
                return false;
            }
        });
        logger.info("preOrder={}", builder.toString());
        // 中序遍历
        logger.info("================== [in] ==================");
        builder.delete(0, builder.length());
        bst.inOrder(new Visitor<>() {
            @Override
            protected boolean visit(Integer element) {
                builder.append(element).append(" ");
                return false;
            }
        });
        logger.info("inOrder={}", builder.toString());
        // 后序遍历
        logger.info("================== [post] ==================");
        builder.delete(0, builder.length());
        bst.postOrder(new Visitor<>() {
            @Override
            protected boolean visit(Integer element) {
                builder.append(element).append(" ");
                return false;
            }
        });
        logger.info("postOrder={}", builder.toString());
    }
}
